package com.baiyang.app.ui.read;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.baiyang.app.R;
import com.baiyang.app.databinding.ActivityReadPageBinding;
import com.baiyang.app.ui.read.commentList.CommentActivity;

public class ReadPageActivity extends AppCompatActivity {

    private ActivityReadPageBinding binding;
    private String articleId;

    private ReadPageViewModel viewModel;
    private ReadPageService service;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_read_page);
        articleId = getIntent().getStringExtra("articleId");
        viewModel = new ViewModelProvider(this).get(ReadPageViewModel.class);
        service = new ReadPageService(getApplicationContext(),viewModel);
        initHandler();
        binding.showPage.findViewById(R.id.follow).setOnClickListener(e->{
            service.follow(articleId);
        });
        binding.showPage.findViewById(R.id.back).setOnClickListener(e->{
            finish();
        });
        // 点击评论按钮
        binding.showPage.findViewById(R.id.comment).setOnClickListener(e->{
            Intent intent = new Intent();
            intent.setClass(getApplicationContext(), CommentActivity.class);
            intent.putExtra("articleId",articleId);
            startActivity(intent);
        });
    }

    private void initHandler() {
        viewModel.getContentResult().observe(this,e->{
            String result = e.getString("result");
            if("SUCCESS".equals(result)){
                String str = e.getString("data");
                binding.showPage.setStr(str);
            }else{
                String detail = e.getString("detail");
                if(detail.equals("balance")){
                    Toast.makeText(getApplicationContext(),"余额不足请充值",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getApplicationContext(),detail,Toast.LENGTH_SHORT).show();
                }
            }
        });
        viewModel.getFollowStateResult().observe(this,e->{
            String result = e.getString("result");
            if("SUCCESS".equals(result)){
                Boolean state = e.getBoolean("data");
                if(!state){
                    binding.showPage.findViewById(R.id.follow).setVisibility(View.VISIBLE);
                }
            }else{
                String detail = e.getString("detail");
                if(detail.equals("balance")){
                    Toast.makeText(getApplicationContext(),"余额不足请充值",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getApplicationContext(),detail,Toast.LENGTH_SHORT).show();
                }
            }
        });
        viewModel.getFollowResult().observe(this,e->{
            String result = e.getString("result");
            if("SUCCESS".equals(result)){
                Toast.makeText(getApplicationContext(),"关注成功",Toast.LENGTH_SHORT).show();
                binding.showPage.findViewById(R.id.follow).setVisibility(View.GONE);
            }else{
                String detail = e.getString("detail");
                if(detail.equals("balance")){
                    Toast.makeText(getApplicationContext(),"余额不足请充值",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getApplicationContext(),detail,Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        service.getContent(articleId);
        service.getFollowState(articleId);
        binding.showPage.findViewById(R.id.follow).setVisibility(View.GONE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        binding = null;
    }
}